﻿using Microsoft.SharePoint.Client;
using System;
using System.IO;
using System.Net;

namespace Office365.SharePointOnline.Cmdlets
{
    [System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Add, "Solution")]
    public class Add_Solution : System.Management.Automation.PSCmdlet
    {
        [System.Management.Automation.Parameter(Position = 0, Mandatory = true)]
        public string Url;

        [System.Management.Automation.Parameter(Position = 1, Mandatory = false)]
        public string User;

        [System.Management.Automation.Parameter(Position = 2, Mandatory = false)]
        public string Password;

        [System.Management.Automation.Parameter(Position = 3, Mandatory = false)]
        public string AuthenticationMethod;

        // use only if onpremise authentication
        [System.Management.Automation.Parameter(Position = 4, Mandatory = false)]
        public string Domain;

        // set the path to the package
        // example Uploader
        [System.Management.Automation.Parameter(Position = 5, Mandatory = false)]
        public string PackagePath;

        // set the relative path to the package
        // example: "/sites/test/_catalogs/solutions/Uploader.wsp"
        [System.Management.Automation.Parameter(Position = 6, Mandatory = false)]
        public string RelativePathPackage;

        protected override void ProcessRecord()
        {
            // check parameters
            ClientContext context = new ClientContext(Url);
            ICredentials credentials;
            Web web = context.Web;
            this.WriteVerbose("Logging on.");
            if (string.IsNullOrEmpty(Url))
            {
                this.WriteObject("Error: url is not set");
            }
            else
            {
                if (!string.IsNullOrEmpty(Password) && !string.IsNullOrEmpty(User))
                {
                    AuthenticationHelper authenticationHelper = new AuthenticationHelper() { AuthenticationMethod = AuthenticationMethod, Domain = Domain, Password = Password, User = User };
                    credentials = authenticationHelper.GetCredentials();
                    if (credentials != null)
                    {
                        context.Credentials = credentials as ICredentials;
                    }
                    else
                    {
                        throw new NullReferenceException("No credentials returned.");
                    }
                }
                else
                {
                    this.WriteVerbose("Reusing existing credentials");
                }

                this.WriteVerbose("Uploading solution to the solution gallery.");
                context.Load(web);
                context.ExecuteQuery();

                UploadSolution(context, web);
            }
        }

        // based on: http://msdn.microsoft.com/en-us/library/ee956524%28office.14%29.aspx#SP2010ClientOMOpenXml_Uploading
        private void UploadSolution(ClientContext context, Web web)
        {
            using (FileStream fileStream = new FileStream(PackagePath, FileMode.Open))
            {
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, RelativePathPackage, fileStream, true);
            }
        }
    }
}
